﻿
/*----------------------------------------------------------------
 * 创建时间：2023/8/4 13:56:32
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Core.Contrls 
 * 描  述：坐标工工具类
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using MaterialDesignThemes.Wpf;
using Minotaur.App.Domains.Enum;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;

namespace Minotaur.App.Core.Contrls
{
    /// <summary>
    /// 坐标工工具类
    /// </summary>
    public class CoordinateUilts
    {

        /// <summary>
        /// 计算开始节点的坐标
        /// </summary>
        /// <param name="propType"></param>
        /// <param name="Coordinate"></param>
        /// <param name="controlWidth"></param>
        /// <param name="controlHeight"></param>
        /// <returns></returns>
        public static double[] ComputeStartNodeCoordinate(PropType propType, string Coordinate, double controlWidth, double controlHeight, string tag)
        {
            double[] coordinates = new double[] { 0, 0 };
            switch (propType)
            {
                case PropType.StartNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 0.5;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + 0.5;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 0.5;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight - 0.5;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.ConditionsNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 9;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + 9;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 9;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight - 9;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.ApprovalNode:
                case PropType.AccessNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 1;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1];
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 1;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.BranchNode:
                case PropType.EndBranchNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 9;
                                    var top = btnCoordinates[1] + (controlHeight / 2) + 0.5;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + 9;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 9;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight - 9;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.EndNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 0.5;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + 0.5;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 0.5;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight - 0.5;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                default:
                    break;
            }
            return coordinates;
        }

        /// <summary>
        /// 计算结束节点的坐标
        /// </summary>
        /// <param name="propType"></param>
        /// <param name="Coordinate"></param>
        /// <param name="controlWidth"></param>
        /// <param name="controlHeight"></param>
        /// <returns></returns>
        public static double[] ComputeEndNodeCoordinate(PropType propType, string Coordinate, double controlWidth, double controlHeight, string tag)
        {
            double[] coordinates = new double[] { 0, 0 };
            switch (propType)
            {
                case PropType.StartNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0];
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1];
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 2;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight - 2;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.EndNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 2;
                                    var top = btnCoordinates[1] + (controlHeight / 2) + 2;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + 4;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 2;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2) + 2;
                                    var top = btnCoordinates[1] + controlHeight - 2;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.ConditionsNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 6;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2) - 2;
                                    var top = btnCoordinates[1] + 8;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 6;
                                    var top = btnCoordinates[1] + (controlHeight / 2) - 2;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2) - 1;
                                    var top = btnCoordinates[1] + controlHeight - 6;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.ApprovalNode:
                case PropType.AccessNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 1;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1];
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 1;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2);
                                    var top = btnCoordinates[1] + controlHeight;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                case PropType.BranchNode:
                case PropType.EndBranchNode:
                    {
                        switch (tag)
                        {
                            case "left":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + 6;
                                    var top = btnCoordinates[1] + (controlHeight / 2);
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "top":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2) - 2;
                                    var top = btnCoordinates[1] + 8;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "right":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + controlWidth - 6;
                                    var top = btnCoordinates[1] + (controlHeight / 2) - 2;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            case "bottom":
                                {
                                    double[] btnCoordinates = JsonConvert.DeserializeObject<double[]>(Coordinate);
                                    var left = btnCoordinates[0] + (controlWidth / 2) - 1;
                                    var top = btnCoordinates[1] + controlHeight - 6;
                                    coordinates = new double[] { left, top };
                                }
                                break;
                            default: break;
                        }
                    }
                    break;
                default:
                    break;
            }
            return coordinates;
        }

        /// <summary>
        /// 获取所有的样式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T)
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child));
            }
            return childList;
        }

        /// <summary>
        /// 设置背景色
        /// </summary>
        /// <param name="userControl"></param>
        public static List<Button> SetBackground(UserControl userControl)
        {
            List<Button> btns = CoordinateUilts.GetChildObjects<Button>(userControl.Parent).Where(o => !string.IsNullOrEmpty(o.Tag + "") && o.Content is null).ToList();
            foreach (var item in btns)
            {
                PackIcon packIcon = new PackIcon()
                {
                    Background = new SolidColorBrush(Colors.Snow),
                    Kind = PackIconKind.Close,
                    Foreground = new SolidColorBrush(Colors.Snow),
                };
                item.Content = packIcon;
            }
            return btns;
        }
    }
}
